package com.cn.demo4.servlet.admin.hr;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.model.HrModel;
import com.cn.demo4.model.PageModel;
import com.cn.demo4.service.IHrService;
import com.cn.demo4.service.impl.HrServiceImpl;
import com.cn.demo4.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/hr/findAll")
public class HrFindAllServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String title = req.getParameter("title");
        String href = req.getParameter("href");
        String status = req.getParameter("status");
        status = status == null ? "-1" : status;
        String contentOne = req.getParameter("contentOne");
        String contentTwo = req.getParameter("contentTwo");
        String curPage = req.getParameter("curPage");
        String pageSize = req.getParameter("pageSize");

        HrModel hrModel = new HrModel();
        hrModel.setHref(href);
        hrModel.setTitle(title);
        hrModel.setStatus(Tool.stringToInt(status));
        hrModel.setContentOne(contentOne);
        hrModel.setContentTwo(contentTwo);
        PageModel pageModel = new PageModel();
        pageModel.setCurPage(Tool.toCurPage(curPage));
        pageModel.setPageSize(Tool.toPageSize(pageSize));
        hrModel.setPageModel(pageModel);

        IHrService iHrService = new HrServiceImpl();
        List<HrModel> list = iHrService.findAll(hrModel);
        int count = iHrService.getCount(hrModel);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
